<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <div th:replace="header::common"></div>
    <style>
        .center {
            padding-left: 5px;
            padding-right: 5px;
            padding-top: 5px;
        }
        .pagination {
            display: inline-block;
            padding-left: 0px;
            margin: 0px 0px;
            border-radius: 4px;
        }
        .table {
            width: 100%;
            max-width: 100%;
            margin-bottom: 10px;
            margin-top: 10px;
        }
    </style>
</head>
<body class="center">
<a class="btn btn-sm btn-primary" href="/user/toAdd">添加</a>
<a class="btn btn-sm btn-danger" th:onclick="deleteAll()">批量删除</a>
<!--搜索框-->
<div th:if="${session.user.role==1}">
    <label></label><br>
    <label for="searchName">查询条件：</label><br>
    <label style="margin-left: 20px;">名字:</label>
    <input type="text" id="searchName" name="searchName" placeholder="请输入名字" th:value="${searchName}">
    <label style="margin-left: 20px;">手机号:</label>
    <input type="text" id="searchPhone" name="searchPhone" placeholder="请输入手机号" th:value="${searchPhone}">
    <label style="margin-left: 20px;">状态:</label>
    <input type="radio" name="status" value="0" th:checked="${status == 0 }" style="margin-left: 20px;"> 停用
    <input type="radio" name="status" value="1" th:checked="${status == 1 }" style="margin-left: 20px;"> 正常
    <input type="radio" name="status" value="3" th:checked="${status == 3  or status == null}" style="margin-left: 20px;"> 不选
    <button onclick="search()" style="
    background-color: #4CAF50; /* 绿色背景 */
    color: white; /* 白色文字 */
    padding: 10px 10px; /* 内部填充 */
    border-radius: 5px; /* 圆角边框 */
    text-align: center; /* 文字居中 */
    display: inline-block; /* 行内块显示 */
    font-size: 12px; /* 字体大小 */
    margin-left: 40px; /* 左边距 */
    box-shadow: 0 4px #999; /* 阴影效果 */
">
        查询
    </button>
</div>
<table class="table table-striped table-bordered table-hover table-condensed">
    <tr class="warning">
        <td><input type="checkbox" id="checkAll"></td>
        <td>ID</td>
        <td>名字</td>
        <td>密码</td>
        <td>昵称</td>
        <td>邮箱</td>
        <td>手机号</td>
        <td>头像</td>
        <td>更新时间</td>
        <td>角色</td>
        <td>状态</td>
        <td>操作</td>
    </tr>
    <tr th:each="user:${pageInfo.list}">
        <td><input type="checkbox" th:value="${user.id}" class="cb"></td>
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.password}"></td>
        <td th:text="${user.nickName}"></td>
        <td th:text="${user.email}"></td>
        <td th:text="${user.phone}"></td>
        <td><img width="40px" height="20px" th:src="|/img/${user.image}|"></td>
        <td th:text="${#dates.format(user.gmtModified, 'yyyy-MM-dd HH:mm')}"></td><!-- HH-24制，hh12制 !-->
        <td>
            <a class="layui-btn-radius btn-sm btn-warning" th:if="${user.role==1}">管理员</a>
            <a class="layui-btn-radius btn-sm btn-success" th:if="${user.role==2}">用户</a>
        </td>
        <td>
            <a class="layui-btn-radius btn-sm btn-warning" th:if="${user.status==0}">禁用</a>
            <a class="layui-btn-radius btn-sm btn-success" th:if="${user.status==1}">正常</a>
        </td>
        <!--<td><a class="btn btn-danger"th:href="|/user/deleteById?id=${user.id}|">删除</a></td>-->
        <td>
            <a class="layui-btn-radius btn-danger btn-sm" th:onclick="|deleteById(${user.id})|">删除</a>
            <a class="layui-btn-radius btn-primary btn-sm" th:href="|/user/toUpdate?id=${user.id}|">编辑</a>
        </td>
    </tr>
</table>
<nav aria-label="Page navigation">
    <ul class="pagination">
        <!--上一页-->
        <th:block th:if="${pageInfo.pageNo==1}">
            <li class="disabled">
                <a href="#" aria-label="Previous">
                    <span aria-hidden="true">上一页</span>
                </a>
            </li>
        </th:block>
        <th:block th:if="${pageInfo.pageNo!=1}">
            <li>
                <a th:href="|/user/selectByPage?pageNo=${pageInfo.pageNo-1}|" aria-label="Previous">
                    <!--<span aria-hidden="true">&laquo;</span>-->
                    <span aria-hidden="true">上一页</span>
                </a>
            </li>
        </th:block>

        <li th:if="${pageInfo.totalPage>=1}" th:class="${pageInfo.pageNo==page}?active:''" th:each="page:${#numbers.sequence(1, pageInfo.totalPage)}">
            <!--<a th:href="|${pageInfo.pageNo==page?'#':'/user/selectByPage?pageNo=${page}'}|" th:text="${page}"></a>-->
            <th:block th:if="${pageInfo.pageNo==page}">
                <a href="#" th:text="${page}"></a>
            </th:block>
            <th:block th:if="${pageInfo.pageNo!=page}">
                <a th:href="|/user/selectByPage?pageNo=${page}|" th:text="${page}"></a>
            </th:block>
        </li>

        <!--下一页-->
        <th:block th:if="${pageInfo.pageNo==pageInfo.totalPage}">
            <li class="disabled">
                <a href="#" aria-label="Next">
                    <span aria-hidden="true">下一页</span>
                </a>
            </li>
        </th:block>
        <th:block th:if="${pageInfo.pageNo!=pageInfo.totalPage}">
            <li>
                <a th:href="|/user/selectByPage?pageNo=${pageInfo.pageNo+1}|" aria-label="Next">
                    <!-- <span aria-hidden="true">&raquo;</span>-->
                    <span aria-hidden="true">下一页</span>
                </a>
            </li>
        </th:block>
    </ul>
</nav>
<script>
    $(function (){
        $('#checkAll').click(function (){
            $('.cb').prop('checked', this.checked);
        });
    });

    function deleteAll() {
        layer.confirm('您确认要删除么?', function() {
            let array = new Array();
            $('.cb:checked').each(function() {
                array.push($(this).val());
            })
            if (array.length==0){
                mylayer.errorMsg('至少要选择一个');
                return;
            }
            location.href = '/user/deleteAll?ids=' + array;
        });
    }

    function deleteById(id){
        layer.confirm('您确定要删除吗？',function (){
            location.href='/user/deleteById?id='+id;
        });
        // let idDelete=window.confirm('您确定要删除吗？');
        // if (idDelete){
        //     location.href='/user/deleteById?id='+id;
        // }
    }
    function search() {
        var searchName = encodeURIComponent(document.getElementById("searchName").value);
        var searchPhone = encodeURIComponent(document.getElementById("searchPhone").value);
        var statusRadios = document.getElementsByName("status");
        // 定义一个变量来存储选中的单选框的值
        var selectedStatusValue = null;
        // 遍历单选框元素，找到被选中的单选框
        for (var i = 0; i < statusRadios.length; i++) {
            if (statusRadios[i].checked) {
                selectedStatusValue = statusRadios[i].value;
                break;  // 找到被选中的单选框后结束循环
            }
        }
        location.href='/user/selectByPage?name=' + searchName + "&phone=" + searchPhone + "&status=" + selectedStatusValue;
    }
</script>
</body>
</html>
